# shutil --- 高阶文件操作
import shutil

# 将 bar 复制到 bar1
print(shutil.copyfileobj(open('bar', 'r'), open('bar1', 'w')))  # None
print(shutil.copyfile('bar', 'bar1'))  # bar1

# 将 bar 复制给 bar
# print(shutil.copyfile('bar', 'bar')) 导致异常 exception shutil.SameFileError

# 复制权限
print(shutil.copymode('bar', 'bar1'))  # None

# 复制权限位、访问时间、修改时间等
print(shutil.copystat('bar', 'bar1'))  # None

# 将文件 src 拷贝到文件或目录 ds
print(shutil.copy('bar', 'bar2'))  # bar2
print(shutil.copy('bar', 'bar3'))  # bar3

# 复制完整的目录树
print(shutil.copytree('tst', 'tst2'))  # tst2
# 删除完整的目录树
print(shutil.rmtree('tst2'))  # None

# 移动文件 or 目录
print(shutil.move('bar3', 'bar'))  # bar

# 返回给定路径的磁盘使用统计数据
print(shutil.disk_usage('D:'))  # usage(total=242335346688, used=217244798976, free=25090547712)

# 修改所有者与组
# print(shutil.chown('bar', 'm1761'))

# 返回可执行文件的路径
print(shutil.which('python'))  # D:\SourceCode\original_tools\venv\Scripts\python.EXE

# 获取终端窗口的尺寸
print(shutil.get_terminal_size())  # os.terminal_size(columns=80, lines=24)

# # 压缩 归档
print(shutil.make_archive('tst-tar', 'tar', 'tst'))  # D:\SourceCode\original_tools\built_in\shutil\tst-tar.tar

# 解包
print(shutil.unpack_archive('tst-tar.tar'))  # None

# 返回支持的归档格式列表
print(
    shutil.get_archive_formats())  # [('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('xztar', "xz'ed tar-file"), ('zip', 'ZIP file')]

print(
    shutil.get_unpack_formats())  # [('bztar', ['.tar.bz2', '.tbz2'], "bzip2'ed tar-file"), ('gztar', ['.tar.gz', '.tgz'], "gzip'ed tar-file"), ('tar', ['.tar'], 'uncompressed tar file'), ('xztar', ['.tar.xz', '.txz'], "xz'ed tar-file"), ('zip', ['.zip'], 'ZIP file')]
